﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMM.Tapioca.Linkbase;
using WMM.Tapioca.Instance;
using WMM.Tapioca.Taxonomy;

namespace WMM.Tapioca.Formula.Filters
{
    /// <summary>
    /// The concept period-type filter can be used to match facts based on whether they report values
    /// for duration-type or instant-type concepts, as determined by the @xbrli:periodType attribute. 
    /// </summary>
    public class ConceptPeriodTypeFilter : Filter
    {
        private string periodType;

        /// <returns> The @periodType attribute is the concept period-type filter. </returns>
        public string PeriodType
        {
            get { return periodType; }
            set { periodType = value; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public ConceptPeriodTypeFilter(Resource resource)
            : base(resource)
        {
            periodType = (string)resource.ResourceXElement.Attribute("periodType") ?? string.Empty;    
        }


        public override List<FactItem> EvaluateFilter(VariableSet variableSet, List<FactItem> itemsToFilter, DTS dts)
        {
            this.dts = dts;

            List<FactItem> itemListTemp = new List<FactItem>();

            foreach (FactItem item in itemsToFilter)
            {
                if (Complement ? !item.Concept.PeriodTypeString.Equals(PeriodType) : item.Concept.PeriodTypeString.Equals(PeriodType))
                {
                    itemListTemp.Add(item);
                }
            }

            return itemListTemp;
        }
    }
}
